#pragma once
#include "PlayerBasic.h"
#include "PlayerUtility.h"

static const wchar_t* C_APP_HPCPlayer = L"UPUPOOPlayer.exe";

class CPlayerHPC :
	public CPlayerBasic
{
public:
	CPlayerHPC(RECT rc) {
		WCHAR szFilePath[MAX_PATH + 1] = { 0 };
		GetModuleFileName(NULL, szFilePath, MAX_PATH);
		std::wstring exepath = szFilePath;
		playerpath_ = exepath.substr(0, exepath.find_last_of(L'\\') + 1);
		volume_ = GetCfgVolumn();
		playspeed_ = GetCfgPlaySpeed();
		playrepeattype_ = GetCfgPlayRepeatType();
		mainwindow_ = AfxGetMainWnd()->GetSafeHwnd();
		rcParent_ = rc;
	}
	~CPlayerHPC() {
		KillAppByExeName(C_APP_HPCPlayer);
	}

	virtual void CloseVoid() {
		KillAppByExeName(C_APP_HPCPlayer);
	}

	virtual void PlayFullSize(const wchar_t* videosize) {
		//RECT rc;
		//::GetWindowRect((HWND)mainwindow_, &rc);
		::SetParent((HWND)playerhandle_, (HWND)mainwindow_);
		ShowWindow((HWND)mainwindow_, SW_SHOWNORMAL);

		/*CString out;
		out.Format()*/

		CString strMsg;
		strMsg.Format(L"0|0|%d|%d", rcParent_.right - rcParent_.left, rcParent_.bottom - rcParent_.top);

		OutputDebugString(L"\nPlayFullSize:");
		OutputDebugString(strMsg);

		CString data;
		data.Format(L"videoSizeChange:%s", (LPCWSTR)strMsg);
		SendCopyDataMsgByHWND(playerhandle_, data);

		playstate_ = 1;

		__super::PlayFullSize(videosize);
	}

	virtual void SetHandle(const wchar_t* handle) {
		CString strHandle = handle;
		CString handle1 = strHandle.Left(strHandle.Find(L'|'));
		CString handle2 = strHandle.Right(strHandle.ReverseFind(L'|'));

		playerhandle_ = (HANDLE)_wtol(handle1);
		playervideohandle_ = (HANDLE)_wtol(handle2);

		CString data;
		data.Format(L"%dPlayerVideoHandle:%d", screenid_, (LPCWSTR)handle2);
		SendCopyDataMsgByHWND(playerhandle_, data);
	}

	virtual void SetUrl(const wchar_t* url) {
		__super::SetUrl(url);

		if (false == created_) {
			CString param;
			param.Format(L"MainHandle:%d;", mainwindow_);
			param.AppendFormat(L"URL:%s;", url);
			param.AppendFormat(L"PlayRepeatType:%d;", playrepeattype_);
			//param.AppendFormat(L"PlayRepeatType:1;", playrepeattype_);
			if (primary_) {
				param.AppendFormat(L"Volume:%d;", GetCfgVolumn());
			} else {
				param.Append(L"Volume:0;");
			}
			param = L"\"" + param + L"\"";
			::ShellExecute(NULL, L"open", std::wstring(playerpath_ + C_APP_HPCPlayer).c_str(), param, NULL, SW_HIDE);

			created_ = true;
		} else {
			CString param;
			param.Format(L"URL:%s", url);
			SendCopyDataMsgByHWND(playerhandle_, param);
		}
	}

	virtual void SetPlaySpeed(float value) {
		__super::SetPlaySpeed(value);

		CString data;
		data.Format(L"PlaySpeed:%.2f", value);
		SendCopyDataMsgByHWND(playerhandle_, data);
	}

	virtual void SetPlayState(int value) {
		__super::SetPlayState(value);

		CString data;
		data.Format(L"PlayState:%d", value);
		SendCopyDataMsgByHWND(playerhandle_, data);
	}

	virtual void SetVolumn(int value) {
		__super::SetVolumn(value);

		CString data;
		data.Format(L"Volume:%d", value);
		SendCopyDataMsgByHWND(playerhandle_, data);
	}

	virtual void SetPlayRepeatType(int value) {
		CString data;
		data.Format(L"PlayRepeatType:%d", value);
		SendCopyDataMsgByHWND(playerhandle_, data);

		__super::SetPlayRepeatType(value);
	}

private:
	bool created_ = false;
	std::wstring playerpath_;
	RECT rcParent_;
};

